Grokking Google Coding Interview
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Introduction
Who Should Take This Course?
Google Easy
Add Binary (easy)
Solution: Add Binary
Valid Mountain Array (easy)
Solution: Valid Mountain Array
Set Mismatch (easy)
Solution: Set Mismatch
Is Subsequence (easy)
Solution: Is Subsequence
Length of Last Word (easy)
Solution: Length of Last Word
Search Insert Position (easy)
Solution: Search Insert Position
Repeated Substring Pattern (easy)
Solution: Repeated Substring Pattern
Monotonic Array (easy)
Solution: Monotonic Array
Count Nodes Equal to Average of Subtree (easy)
Solution: Count Nodes Equal to Average of Subtree
Find the Difference (easy)
Solution: Find the Difference
How Many Numbers Are Smaller Than the Current Number (easy)
Solution: How Many Numbers Are Smaller Than the Current Number
Binary Tree Paths (easy)
Solution: Binary Tree Paths
Find the Difference of Two Arrays (easy)
Solution: Find the Difference of Two Arrays
Merge Strings Alternately (easy)
Solution: Merge Strings Alternately
Reverse a LinkedList (easy)
Solution: Reverse a LinkedList
Gray Code (medium)
Solution: Gray Code
2 Keys Keyboard (medium)
Solution: 2 Keys Keyboard
Arithmetic Slices (medium)
Solution: Arithmetic Slices
Unique Length-3 Palindromic Subsequences (medium)
Solution: Unique Length-3 Palindromic Subsequences
Minimum Difference Between Largest and Smallest Value in Three Moves (medium)
Solution: Minimum Difference Between Largest and Smallest Value in Three Moves
Letter Combinations of a Phone Number (medium)
Solution: Letter Combinations of a Phone Number
01 Matrix (medium)
Solution: 01 Matrix
Maximum Points You Can Obtain from Cards (medium)
Solution: Maximum Points You Can Obtain from Cards
Strobogrammatic Number II (medium)
Solution: Strobogrammatic Number II
Maximum Length of Semi-Decreasing Subarrays (medium)
Solution: Maximum Length of Semi-Decreasing Subarrays
Minimum Area Rectangle (medium)
Solution: Minimum Area Rectangle
Count Unreachable Pairs of Nodes in an Undirected Graph (medium)
Solution: Count Unreachable Pairs of Nodes in an Undirected Graph
Single Number II (medium)
Solution: Single Number II
4Sum II (medium)
Solution: 4Sum II
Furthest Building You Can Reach (medium)
Solution: Furthest Building You Can Reach
Minimizing Array After Replacing Pairs With Their Product (medium)
Solution: Minimizing Array After Replacing Pairs With Their Product
Fair Distribution of Cookies (medium)
Solution: Fair Distribution of Cookies
Find Leaves of Binary Tree (medium)
Solution: Find Leaves of Binary Tree
Rotate Image (medium)
Solution: Rotate Image
Combination Sum (medium)
Solution: Combination Sum
Implement Trie (Prefix Tree) (medium)
Solution: Implement Trie (Prefix Tree)
Number of Islands (medium)
Solution: Number of Islands
Edit Distance (medium)
Solution: Edit Distance
Container With Most Water (Medium)
Solution: Container With Most Water
Meeting Rooms II (medium)
Solution: Meeting Rooms II
Shortest Subarray with Sum at Least K (hard)
Solution: Shortest Subarray with Sum at Least K
Reverse Pairs (hard)
Solution: Reverse Pairs
Nth Magical Number (hard)
Solution: Nth Magical Number
Candy (hard)
Solution: Candy
Arithmetic Slices II - Subsequence (hard)
Solution: Arithmetic Slices II - Subsequence
Burst Balloons (hard)
Solution: Burst Balloons
Sum of Distances in Tree (hard)
Solution: Sum of Distances in Tree
Maximum Score of a Good Subarray (hard)
Solution: Maximum Score of a Good Subarray
Trapping Rain Water (hard)
Solution: Trapping Rain Water
First Missing Positive (hard)
Solution: First Missing Positive
Solution: Is Subsequence

Problem Statement

Given two strings s and t, return true if the string s is a subsequence of the string t. Otherwise, return false.

A subsequence of a string is a new string that can be formed from the original string by eliminating some (can be zero) of the characters without changing the order of the remaining characters.

Examples

  • Example 1:

    • Input: s = "hello", t = "xyhealoloo"
    • Expected Output: true
    • Justification: By removing "x", "y", "a", "o", and the second "o" from t, s can be obtained while maintaining the original order.
  • Example 2:

    • Input: s = "axc", t = "ahbgdc"
    • Expected Output: false
    • Justification: There is no way to remove characters from t to achieve s without altering the sequence of "axc".
  • Example 3:

    • Input: s = "abc", t = "aabc"
    • Expected Output: true
    • Justification: By removing the first "a" in t, s is formed while preserving the character sequence.

Solution

To solve this problem, we adopt a two-pointer technique that iteratively compares characters from s and t. One pointer is used to traverse s, and another pointer for t. The idea is to advance both pointers when characters match, indicating that part of the subsequence has been found. When characters do not match, only the pointer for t advances, searching for the next potential matching character.

This method is effective because it linearly scans through both strings simultaneously, ensuring that the relative order of characters in s is maintained in t. By the end of the traversal, if we have successfully navigated through all characters in s, it confirms that s is indeed a subsequence of t.

Step-by-Step Algorithm

  1. Initialize Pointers: Start by initializing two pointers, indexS and indexT, to 0. These pointers will be used to traverse through the strings s and t, respectively.

  2. Traverse Through t: Begin a loop that continues as long as indexT is less than the length of t and indexS is less than the length of s. This loop will go through each character of t to check against s.

  3. Check Character Match: Inside the loop, compare the current character of t (pointed to by indexT) with the current character of s (pointed to by indexS).

    • If the characters match, increment indexS by 1 to move to the next character in s. This step is crucial as it indicates that part of the subsequence has been found.
  4. Move to Next Character in t: After the check or in case of a mismatch, increment indexT by 1 to continue scanning through t.

  5. Check Subsequence Found: After exiting the loop, check if indexS equals the length of s. This condition signifies that all characters in s have been found in t in the correct order.

    • If indexS equals the length of s, return true, indicating s is a subsequence of t.
    • If the loop ends and indexS is not equal to the length of s, return false, indicating s is not a subsequence of t.

Algorithm Walkthrough

Let's consider the Input: s = "hello", t = "xyhealoloo"

  • Initial Setup: indexS = 0, indexT = 0.
  1. Step 1: Begin Loop
    • indexT = 0, t[0] = 'x', indexS = 0, s[0] = 'h'. They do not match, so only indexT is incremented.
  2. Step 2: Next Iteration
    • indexT = 1, t[1] = 'y', still no match. Increment indexT.
  3. Step 3: Finding 'h'
    • indexT = 2, t[2] = 'h', matches s[0] = 'h'. Increment both indexS and indexT.
  4. Step 4: Finding 'e'
    • Continue to indexT = 3, t[3] = 'e', matches s[1] = 'e'. Increment both indexS and indexT.
  5. Step 5: Skipping to 'l'
    • indexT increments through 'a', until it finds the first 'l' at indexT = 5. s[2] = 'l' matches, increment both.
  6. Step 6: Finding Second 'l'
    • indexT = 6, t[6] = 'o', no match, increment indexT.
    • indexT = 7, t[7] = 'l', matches s[3] = 'l'. Increment both.
  7. Step 7: Finding 'o'
    • indexT = 8, t[8] = 'o', matches s[4] = 'o'. Increment both.
  8. Step 8: Conclusion
    • Now, indexS = 5, which equals the length of s. This means every character in s has been found in t in order, confirming s is a subsequence of t.
Image

Code

Java
Saved to Cloud

. . . .

Complexity Analysis

Time Complexity

The time complexity of the algorithm is , where (n) is the length of string t. This is because, in the worst case, we traverse the entire string t once, comparing each character with characters from s. The traversal and comparisons for each character occur in constant time.

Space Complexity

The space complexity is . Only a fixed number of variables (indexS and indexT) are used, regardless of the input size. This makes the space complexity constant, as it does not scale with the size of the input strings.

Is Subsequence (easy)
Length of Last Word (easy)
Mark as Completed

On this page

Problem Statement

Examples

Solution

Step-by-Step Algorithm

Algorithm Walkthrough

Code

Complexity Analysis

Time Complexity

Space Complexity